package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.facebook.ads.NativeAdLayout
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.NativeAdBean
import com.horoscope.astrology.zodiac.signs.free.bean.QuoteBean
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.NativeAdUtil


/**
 *  作者    1024
 *  时间    2020/4/26 下午6:19
 *  文件    项目备份
 *  描述    字体可以参照 https://developer.android.google.cn/guide/topics/resources/font-resource
 */
class QuoteAdapter(private val mContext: Context, private var mList: MutableList<QuoteBean>) :
    BaseAdapter() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_quote, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, i: Int) {
        when (viewHolder) {
            is MyViewHolder -> {
                val bean = mList[i]
                if (bean.isAd) {
                    selectAdType(mList[i].nativeAdBean, viewHolder)
                } else {
                    viewHolder.fv.visibility = View.INVISIBLE
                }

                viewHolder.imageView.setImageResource(
                    mContext.resources.getIdentifier(
                        bean.image,
                        "mipmap",
                        mContext.packageName
                    )
                )
                viewHolder.quoteText.text = bean.text
                viewHolder.quoteText.typeface =
                    Typeface.createFromAsset(mContext.assets, "font/" + bean.font)
                viewHolder.author.text = StringBuilder("-" + bean.author)
                viewHolder.author.typeface =
                    Typeface.createFromAsset(mContext.assets, "font/" + bean.font)
                viewHolder.itemView.tag = i
                if (mOnItemClickedListener != null) {
                    viewHolder.key.visibility = View.VISIBLE
                    viewHolder.key.text = "key"
                    viewHolder.key.setOnClickListener {
                        if (!SysUtils.isFastClick()) {
                            mOnItemClickedListener!!.onItemClick(it, i, "key")
                        }
                    }
                    viewHolder.ivBack.setOnClickListener {
                        if (!SysUtils.isFastClick()) {
                            mOnItemClickedListener!!.onItemClick(it, i, "back")
                        }
                    }
                }
            }
        }

    }

    override fun getItemCount(): Int {
        return mList.size
    }

    //  添加Native
    fun addNativeAd(position: Int, bean: NativeAdBean) {
        mList[position].isAd = true
        mList[position].nativeAdBean = bean
        notifyItemChanged(position)
    }

    //  删除Native
    fun removeNativeAd(position: Int) {
        mList[position].isAd = false
        mList[position].nativeAdBean = null
        notifyItemRangeChanged(position,itemCount-position)

    }

    private fun selectAdType(nativeAdBean: NativeAdBean?, holder: MyViewHolder) {
        if (nativeAdBean != null) {
            holder.fv.visibility = View.VISIBLE
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    if (nativeAdBean.facebookNativeAD != null) {
                        NativeAdUtil.inflateFacebookAd(
                            nativeAdBean.facebookNativeAD, holder.fv, "Custom_Medium", "Native_Quote"
                        )
                        holder.fbAd.visibility = View.VISIBLE
                    } else {
                        holder.fv.visibility = View.INVISIBLE
                    }
                }
                ADConfig.ADMOB -> {
                    if (nativeAdBean.googleNativeAd != null) {
                        NativeAdUtil.inflateAdMobAd(
                            nativeAdBean.googleNativeAd!!, holder.fv, "Custom_Medium"
                        )
                        holder.googleAd.visibility = View.VISIBLE
                    } else {
                        holder.fv.visibility = View.INVISIBLE
                    }
                }
            }
        } else {
            holder.fv.visibility = View.INVISIBLE
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivBack: ImageView = itemView.findViewById(R.id.iv_back)
        val quoteText: TextView = itemView.findViewById(R.id.text)
        val author: TextView = itemView.findViewById(R.id.author)
        val url: TextView = itemView.findViewById(R.id.url)
        val root: ConstraintLayout = itemView.findViewById(R.id.root)
        val imageView: ImageView = itemView.findViewById(R.id.img_background)

        val fv: FrameLayout = itemView.findViewById(R.id.linear_root)
        val googleAd: FrameLayout = itemView.findViewById(R.id.ad_frame)
        val fbAd: NativeAdLayout = itemView.findViewById(R.id.native_ad_container)
        val key: TextView = itemView.findViewById(R.id.key)
    }
}